//
//  AppDelegate.m
//  0613coreDataToChat
//
//  Created by dllo on 15/9/21.
//  Copyright (c) 2015年 shuai. All rights reserved.
//

#import "AppDelegate.h"
#import "CoreDataManager.h"

@interface AppDelegate ()

@property(nonatomic, strong)CoreDataManager *manager;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    // 判断版本
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0f) {
        //注册通知设置
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
        //注册通知
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        //系统8.0之前的注册通知的方法
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
    }
    
    self.manager = [CoreDataManager defaults];
    
    
    return YES;
}

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NSLog(@"userInfo    %@", userInfo);
    
    // 收到消息后立刻存入本地
    [self.manager addMessage:userInfo[@"aps"][@"alert"][@"body"] fromName:userInfo[@"payload"] toName:@"郭恒维"];
    
    //触发receive事件  调用响应方法
    [[NSNotificationCenter defaultCenter] postNotificationName:@"receive" object:nil];
    //角标清零
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    
    
    
    
}

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSLog(@"token   %@", deviceToken);
}

-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    NSLog(@"error    %@", error);
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
